import Cookies from "js-cookie";

// 默认教师端
function getTokenKey() {
	var href = location.href;
	return href.indexOf('/teacher-app') > -1 ? 'TeacherCodeCat' : 'StudentCodeCat'
}
const UserKey = "CodeCatMain";
const Belong = "Belong";
const Redirect = "Redirect";

export function getToken() {
	return Cookies.get(getTokenKey()) || null;
}

export function setToken(token) {
	Cookies.set(getTokenKey(), token);
}

export function removeToken() {
	Cookies.remove('TeacherCodeCat');
	Cookies.remove('StudentCodeCat');
}

export function getUserInfo() {
	const info = Cookies.get(UserKey);
	return info ? JSON.parse(info) : {};
}

export function setUserInfo(value) {
	Cookies.set(UserKey, JSON.stringify(value), {
		expires: 30,
	});
}

export function removeUserInfo() {
	Cookies.remove(UserKey);
}

// 机构、校区
export function getBelong() {
	return Cookies.get(Belong) ? Cookies.get(Belong).split("-") : null;
}

export function setBelong(val) {
	if (Array.isArray(val) && val.length) {
		Cookies.set(Belong, val.join("-"));
	}
}

export function removeBelong() {
	Cookies.remove(Belong);
}

export function getRedirect() {
	return Cookies.get(Redirect);
}

export function setRedirect(val) {
	Cookies.set(Redirect, val);
}

export function removeRedirect() {
	Cookies.remove(Redirect);
}